package com.vans.practice.thread;

/**
 * @Description: 测试synchronized, 锁住一个方法, 其他方法是否能被其他线程访问
 * @Note:
 * @Author elle
 * @Date 2020/10/8 12:30
 */
public class SynchronizedTest implements Runnable {

    private int a = 100;

    public static void main(String[] args) {
        SynchronizedTest t1 = new SynchronizedTest();
        Thread t = new Thread(t1);
        t.start();
        t1.f1();
    }

    public void f1() {
        System.out.println("a的值为" + a);
    }

    public synchronized void f2() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a += 100;
        System.out.println("a的值为" + a);
    }

    @Override
    public void run() {
        f2();
    }
}
